#include "TCPServe.hpp"
#include "Protocol.hpp"

#define ENDL printf("\n")

void GetDataFromStdinput(int *x,int *y,char *oper)
{
    std::cout<<"please enter x: ";
    std::cin>>*x;

    std::cout<<"please enter y: ";
    std::cin>>*y;

    std::cout<<"please enter oper: ";
    std::cin>>*oper;
}
int main(int argc,char*argv[])
{
    if(argc!=3)
    {
        LOG(FATAL)<<"请正确传参，例如: ./.exe serveip serveport";
        exit(USAGE_ERR);
    }
    std::string serveip=argv[1];
    uint16_t serveport=std::stoi(argv[2]);
 
    std::unique_ptr<Socket> client=std::make_unique<TCPSocket>();
    
    client->BuildTCPClientSockfd();
    if(client->Connect(serveip,serveport)!=0)
    {
        std::cerr<<"connect error";
        exit(CONNECT_ERR);
    }
    std::unique_ptr<Protocol> protocol =std::make_unique<Protocol>();
    std::string res_buffer;
    while(true)
    {
        int x,y;
        char oper;
        //1.从标准输入读取数据
        GetDataFromStdinput(&x,&y,&oper);
        //2.构建可以直接发送的数据,那就得是一个序列化后的串
        std::string SendString=protocol->BuildRequestString(x,y,oper);
        // std::cout<<"----------封装好的字符串---------"<<std::endl;
        // std::cout<<SendString<<std::endl;
        // std::cout<<"--------------------------------"<<std::endl;
        //3.直接发送
        client->Send(SendString);
        //4.获取响应
        Response res;
        bool ok=protocol->GetResponse(client,res_buffer,&res);
        if(!ok)
            break;
        res.ShowResult();
    }
    client->Close();
    return 0;
}